Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Anybody know "If 0 {" ?

    One of my colleagues asks me this question and I've never seen such a code but it returns some results.

    1) Run this simple code

    * STATA code
    sysuse auto2
    count
    if 0 {
    display make in 1
    }
    if 1 {
    display make in 1
    }


    2) In the result window, we get this

    . clear
    . sysuse auto2
    (1978 Automobile Data)

    . count
    74

    . if 0 {
    . display make in 1
    . }

    . if 1 {
    . display make in 1
    AMC Concordinvalid syntax
    r(198);
    .
    . }
    r(198);


    3) My question is, is there anybody who can explain what "If 0 {" and "If 1 {" mean? I don't know what condition is applied In this if-loop. why do we get no errors when we use "If 0 {" and why do we get r(198) error code if we use "If 1 {"? When do we use such an if-statement using only 0 or 1 without variable name?

    This is my 1st post in this forum and thanks in advance!

    Connor

  • #2
    The code following an if command is executed if and only if its argument evaluates as true and is not executed otherwise. So (the argument in this example being 1 or 0, which manifestly is true or false respectively) it follows that any block

    Code:
    if 1 {
    
    }
    always executes. and the other case never executes. This seems clear-cut but what is a mystery to me is why anyone would write even a single line of code, let alone three, that is designed never to execute.

    However, the code, whose source you perhaps politely do not cite, is very confused, as display certainly does not accept in qualifiers, as your results show. See also the help for display: whatever is not mentioned as a possibility in the syntax is illegal.

    For completeness I note that

    Code:
    display make
    is legal and always interpreted as

    Code:
     
    display make[1]
    which itself is also legal. It may therefore seem strange that

    Code:
     
    display make in 1
    is not legal, but that is the way it is. The best way I can think to see this is that display by default produces a line of output; that could not be true if in were allowed in general, as that qualifier could specify several lines. The same disqualification rules out if.

    This error is completely unconnected with the surrounding if blocks, except that (as above) the error inside if 0 is never uncovered but the error inside if 1 is uncovered.

    count is perfectly legal here and displays the number of observations; it is totally unconnected with any other code apart from that reading in the dataset, which is one with 74 observations.

    So perhaps the only question is why anyone should present this code and to what end.

    More constructively, it can be good practice to write code such as

    Code:
    while 1 {
    
    }
    which sets up a loop which will be exited when and only when some condition is satisfied inside the block.
    Last edited by Nick Cox; 12 Oct 2015, 14:28.

    Comment


    • #3
      Hi Nick,

      First of all, thanks so much for your prompt help! I really appreciate it.
      However, I'm still confused about the true/false argument in this case.

      When I use -display make[1]- instead of -display make in 1-, I got this result.

      . if 0 {
      . display make[1]
      . }

      .
      . if 1 {
      . display make[1]
      AMC Concord
      . }

      Why Stata does not give any result when "If 0 {" statement is used? In this case, exactly what argument was false?

      Comment


      • #4
        Connor,

        In the the statement:
        Code:
        if 0 { 
        display make[1]
        }
        The "argument" (the thing being tested to see if it is true or false) is the zero, which is always false. Everything else (between the curly brackets) is what is to be done if the argument is true (which it never is).

        Regards,
        Joe

        Comment


        • #5
          Overkill:

          Code:
          // an interactive explanation
          // ==========================
          
          // wrap in quietly so you can see which pieces actually execute more clearly
          quietly {
          
          // logical tests
          // -------------
          noisily di "result of logical test is 0 or 1. e.g."
          noisily di "test: 3 == 4; result = " 3 == 4
          noisily di "test: 3 == 3; result = " 3 == 3
          
          // a normal-ish approach
          // ---------------------
          // define a "switch" to decide which block to execute
          // this is often the result of a prior command, but can also be set manually
          local cond = 1
          if `cond' == 0 {
            noisily di "condition not met; this block will execute"
          }
          if `cond' == 1 {
           noisily di "condition met; this block will execute"
          }
          
          // an abnormal approach
          // --------------------
          if 0 {
           noisily di "by definition, this condition is -never- met --> this block will -never- execute."
          }
          if 1 {
           noisily di "by definition, this condition is -always- met --> this block will -always- execute (barring errors)"
          }
          
          }

          Comment


          • #6
            Thanks to Joe and Brendan for fleshing out the argument!

            Comment


            • #7
              Thank you all. Really appreciate it!

              Comment

              Working...
              X